/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.booleanExpression;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A Boolean expression of the form &not;&phi;.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 */
public class Negation extends BooleanExpression
{
	/**
	 * The default symbol that is used as negation symbol. 
	 */
	public static final String NEGATION_SYMBOL = "~";
	
	/**
	 * A constant that is added to the hash value of &phi; to calculate the hash value of
	 * the negation.
	 */
	public static final int HASH_CONSTANT = 1337;

	/*
	 * The formula to negate.
	 */
	private BooleanExpression phi;
	
	/*
	 * The complete size of this expression. 
	 */
	private int size;
	
	/**
	 * Creates a new negation given another expression, so it creates the expression
	 * &not;&phi;.
	 * 
	 * @param phi The formula to negate.
	 */
	public Negation(BooleanExpression phi)
	{
		this.phi = phi;
		this.size = phi.completeSize() + 1;
	}
	
	@Override
	public boolean getTruthValue(Assignment alpha)
	{
		return !phi.getTruthValue(alpha);
	}

	/**
	 * Returns the formula that is negated by this expression.
	 * 
	 * @return The formula that is negated by this expression.
	 */
	public BooleanExpression getPhi()
	{
		return phi;
	}
	
	/**
	 * The hash code of &phi; + the value of {@link #HASH_CONSTANT}, that is {@value #HASH_CONSTANT}.
	 */
	@Override
	public int hashCode()
	{
		return phi.hashCode() + HASH_CONSTANT;
	}
	
	/**
	 * Returns the String <code>negSymbol + phi.toString()</code>.
	 * 
	 * @param negSymbol The symbol that is placed in fron of &phi;.
	 * 
	 * @return A String representation of this negation.
	 */
	public String toString(String negSymbol)
	{
		return negSymbol + phi.toString();
	}
	
	/**
	 * The same as {@link #toString(String)} with the default Symbol {@link #NEGATION_SYMBOL},
	 * that is {@value #NEGATION_SYMBOL}.
	 */
	@Override
	public String toString()
	{
		return toString(NEGATION_SYMBOL);
	}
	
	/**
	 * Returns <code>true</code> if other is also a negation and negates the same
	 * formula (again tested with the <code>equals</code> method).
	 */
	@Override
	public boolean equals(Object other)
	{
		if(other == null || !(other instanceof Negation))
			return false;
		Negation otherNeg = (Negation)other;
		return getPhi().equals(otherNeg.getPhi());
	}
	
	@Override
	public BooleanFunction getFunction()
	{
		return new BooleanFunction()
		{

			@Override
			public boolean getValue(Iterator<Boolean> inputPoint)
			{
				boolean val = inputPoint.next();
				return !val;
			}

			@Override
			public int getDimension()
			{
				return 1;
			}
			
		};
	}
	
	@Override
	public int getArity()
	{
		return 1;
	}

	@Override
	public Iterator<BooleanExpression> iterator()
	{
		return new Iterator<BooleanExpression>()
				{
			boolean returnedElement = false;
			
			@Override
			public boolean hasNext()
			{
				return !returnedElement;
			}

			@Override
			public BooleanExpression next()
			{
				if(!hasNext())
				{
					throw new NoSuchElementException("No more elements in subformulae iteration.");
				}
				returnedElement = true;
				return phi;
			}

			@Override
			public void remove()
			{
				throw new UnsupportedOperationException("It is not allowed to remove elements from a formula.");	
			}
			
				};
	}
	
	@Override
	public int completeSize()
	{
		return size;
	}
	
}
